package com.wrupple.muba.catalogs.client.widgets.editors.composite;

import java.util.Set;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.StackLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.wrupple.muba.catalogs.client.CatalogFieldMap;
import com.wrupple.muba.catalogs.client.cms.service.CatalogManagementSystem;
import com.wrupple.muba.catalogs.client.module.services.logic.CatalogDescriptionService;
import com.wrupple.muba.catalogs.client.module.services.logic.FieldConversionStrategyProvider;
import com.wrupple.muba.catalogs.client.module.services.logic.FieldDescriptionService;
import com.wrupple.muba.catalogs.client.module.services.presentation.GenericFieldFactory;
import com.wrupple.muba.catalogs.domain.CatalogEntry;
import com.wrupple.muba.common.client.FieldConversionStrategy;
import com.wrupple.muba.common.client.JSOHelper;
import com.wrupple.muba.common.shared.State.StorageManager;
import com.wrupple.vegetate.domain.FieldDescriptor;

public class StackCatalogEditor<V extends JavaScriptObject> extends CompositeCatalogEditor<V> {
	
	class EditorValueChangeHandler implements ValueChangeHandler<V>{

		@Override
		public void onValueChange(ValueChangeEvent<V> event) {
			CatalogEntry value = event.getValue().cast();
			Set<String> fieldNames = fields.keySet();
			Object fieldValue;
			FieldConversionStrategy conversion;
			for(String field: fieldNames){
				conversion = conversionService.getFieldConversionStrategy(field, getCatalog());
				fieldValue=JSOHelper.getAttributeAsObject(value, field, conversion);
				if(fieldValue==null){
					jumpTo(field);
					break;
				}
			}
		}
		
	}

	private StackLayoutPanel main;
	protected CatalogFieldMap widgetter;
	public StackCatalogEditor(CatalogManagementSystem cms,StorageManager sm,GenericFieldFactory fieldFactory,FieldDescriptionService fieldService,CatalogFieldMap widgetter, CatalogDescriptionService catalogService, FieldConversionStrategyProvider conversionService) {
		super(cms, sm, fieldService, catalogService, conversionService, fieldFactory);
		main = new StackLayoutPanel(Unit.EM);
		this.widgetter = widgetter;
		initWidget(main);
		setJumpToFirstEmptyField(false);
	}
	

	public void jumpTo(String fieldId) {
		HasValue<Object> field = fields.get(fieldId);
		main.showWidget((Widget)field);
	}

	@Override
	protected void maybeAddField(HasValue<Object> widget,
			FieldDescriptor field) {
		if(main.getWidgetIndex((IsWidget)widget)<0){
			Widget header = generateFieldHeader(field);
			
			setFieldWidget(header, (IsWidget)widget);
		}
		
	}

	
	public void setJumpToFirstEmptyField(boolean jumpToFirstEmptyField) {
		if(jumpToFirstEmptyField){
			super.setCollectFieldChangeEvents(true);
			addValueChangeHandler(new EditorValueChangeHandler());
		}
	}


	public IsWidget setFieldWidget(String name,
			IsWidget field) {
		Widget head = generateFieldHeader(name);
		return setFieldWidget(head, field);
	}
	
	public IsWidget setFieldWidget(Widget head,
			IsWidget field) {
		main.add(field, head, 2.5);
		return field;
	}

	public void removeField(IsWidget field) {
		main.remove(field.asWidget());
	}
	
	private Widget generateFieldHeader(FieldDescriptor field) {
		return generateFieldHeader(field.getName());
	}
	
	private Widget generateFieldHeader(String field) {
		Label l = new Label(field);
		return l;
	}


	

}
